package com.zhihaoliang.busEncy

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.hbmcc.heshenghuo.model.http.bean.TitleAndUrlBean
import com.zhihaoliang.busEncy.adapter.*
import com.zhihaoliang.busEncy.model.BusiRightBean
import com.zhihaoliang.busEncy.model.list.FloorPager
import com.zhihaoliang.busEncy.model.list.FloorPagerVo
import kotlinx.android.synthetic.main.fragment_busi_right.*
import java.util.*
import kotlin.collections.ArrayList


private const val ARG_TYPE = "arg_type"

class BusiLeftFragment : Fragment() {
    private var type: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let { type = it.getString(ARG_TYPE) ?: "" }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_busi_right, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val layoutManager = VirtualLayoutManager(view.context)
        rvRight.setLayoutManager(layoutManager)
        val delegateAdapter = DelegateAdapter(layoutManager, true)
        rvRight.adapter = delegateAdapter
        val adapters: MutableList<DelegateAdapter.Adapter<*>> = LinkedList()
        addTitle("Banner模版", adapters)
        initListData(adapters)
        initListAnchor(adapters)
        addTitle("业务卡片模板", adapters)
        init01(adapters)
        delegateAdapter.setAdapters(adapters)


    }

    private fun initListData(adapters: MutableList<DelegateAdapter.Adapter<*>>) {
        val floorPagers: MutableList<FloorPager> = ArrayList()
        for (index in 0 until 5) {
            floorPagers.add(
                FloorPager(
                    "主标题大小$index",
                    "副标题好个字",
                    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607955798741&di=d246b0966dce8e798dc8b8f6cbcfa030&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F10%2F01%2F01300000091985121196015965747.jpg",
                    "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607955798741&di=d246b0966dce8e798dc8b8f6cbcfa030&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F10%2F01%2F01300000091985121196015965747.jpg",
                    "https://www.baidu.com/",
                )
            )
        }


        val listNew: MutableList<FloorPagerVo> = ArrayList()
        val listUpdateNew: MutableList<FloorPager> = ArrayList()
        for (item in floorPagers) {
            listUpdateNew.add(item)
        }
        if (listUpdateNew.size % 2 == 1) listUpdateNew.add(FloorPager(isShow = false))
        for (index in 0 until listUpdateNew.size step 2) {
            listNew.add(FloorPagerVo(mutableListOf(listUpdateNew[index], listUpdateNew[index + 1])))
        }

        val busiRightBean = BusiRightBean(
            fModelNum = YWNEW_002,
            list = listNew
        )
        adapters.add(BusRightAdapter(busiRightBean))
    }


    private fun initListAnchor(adapters: MutableList<DelegateAdapter.Adapter<*>>) {
        val busiRightBean = BusiRightBean(
            fModelNum = CUNSTOM_001,
            list = ANCHOR_TXT
        )
        //  adapters.add(BusRightAdapter(busiRightBean))
    }

    private fun init01(adapters: MutableList<DelegateAdapter.Adapter<*>>) {


    }

    private fun addTitle(title: String, adapters: MutableList<DelegateAdapter.Adapter<*>>) {
        val titleAndUrlBean = TitleAndUrlBean()
        titleAndUrlBean.f_FU_TITLE = title
        titleAndUrlBean.f_NAME = title
        titleAndUrlBean.f_IS_TITLE = "1"
        titleAndUrlBean.f_FLOOR_SEAT = "0"
        titleAndUrlBean.f_MORE_NAME = "更多"
        titleAndUrlBean.f_MORE_URL = "https://www.baidu.com/"

        val busiRightBean = BusTitleAdapter(titleAndUrlBean)
        adapters.add(busiRightBean)
    }


    companion object {
        @JvmStatic
        fun newInstance(type: String) =
            BusiLeftFragment().apply {
                arguments = Bundle().apply {
                    Log.e(TAG, TAG)
                    putString(ARG_TYPE, type)
                }
            }
    }
}